const maxHCount = 4;
const maxWCount = 4;

//偏移量
let dirArr = [
    { i: -1, j: 0 }, { i: 1, j: 0 }, { i: 0, j: -1 }, { i: 0, j: 1 },
];

console.log(getRandNameArr());

//连接地图
function conectMap(mapNameArr, index, dirNum, dirNotNull = false) {
    let dir = dirArr[dirNum];
    let nearIndex = {
        i: dir.i + index.i,
        j: dir.j + index.j
    }

    //边界判断
    if (nearIndex.i >= maxHCount || nearIndex.j >= maxWCount || nearIndex.i < 0 || nearIndex.j < 0) return;

    //
    if (dirNotNull && mapNameArr[nearIndex.i][nearIndex.j] === '00000') return;

    let nowMapName = mapNameArr[index.i][index.j].split('');
    let nearMapName = mapNameArr[nearIndex.i][nearIndex.j].split('');

    nowMapName[dirNum] = '1';

    let nearNum;
    if (dirNum == 0) {
        nearNum = 1;
    } if (dirNum == 1) {
        nearNum = 0;
    } if (dirNum == 2) {
        nearNum = 3;
    } if (dirNum == 3) {
        nearNum = 2;
    }

    mapNameArr[index.i][index.j] = nowMapName.join('');
    mapNameArr[nearIndex.i][nearIndex.j] = nearMapName.join('');
}

//随机连接地图
function randEmptyMap(mapNameArr) {
    for (let i = 0; i < maxHCount; i++) {
        for (let j = 0; j < maxWCount; j++) {
            let mapName = mapNameArr[i][j];
            if (mapName != '00000') continue;

            let dirNum = parseInt(Math.random() * dirArr.length);
            conectMap(mapNameArr, { i, j }, dirNum, true);
        }
    }
}

//生成地图
function getRandNameArr() {
    let { mapArr, stIndex } = randBaseMap();

    let mapNameArr = [];
    for (let i = 0; i < maxHCount; i++) {
        mapNameArr[i] = [];
        for (let j = 0; j < maxWCount; j++) {
            mapNameArr[i][j] = '00000';
        }
    }

    for (let i = 0; i < maxHCount; i++) {
        for (let j = 0; j < maxWCount; j++) {
            if (!mapArr[i][j]) continue;
            for (let dirNum = 0; dirNum < dirArr.length; dirNum++) {
                conectMap(mapNameArr, { i, j }, dirNum);
            }
        }
    }

    randEmptyMap(mapNameArr);

    return mapNameArr;
}

//生成基础随机数组
function randBaseMap() {
    let mapCount = 8; //地图块数量
    let mapArr = [];

    for (let i = 0; i < maxHCount; i++) {
        mapArr[i] = [];
        for (let j = 0; j < maxWCount; j++) {
            mapArr[i][j] = 0;
        }
    }

    //随机一个起点
    let stIndex = {
        i: parseInt(Math.random() * maxHCount),
        j: parseInt(Math.random() * maxWCount)
    }

    let nextArr = setMap(mapArr, stIndex);
    mapCount--;

    while (mapCount && nextArr.length > 0) {
        let randNum = nextArr[parseInt(Math.random() * nextArr.length)];
        let nextIndex = nextArr.splice(randNum, 1)[0];

        let nearArr = setMap(mapArr, nextIndex);
        if (nextArr) {
            mapCount--;
            nextArr = uniqNextArr([...nearArr, ...nextArr]);
        }
    }

    return { mapArr, stIndex };
}

//去重
function uniqNextArr(nextArr) {
    let tag = {};
    let arr = [];
    for (let index of nextArr) {
        let num = index.i * maxHCount + index.j * maxWCount;
        if (!tag[num]) {
            tag[num] = 1;
            arr.push(index);
        }
    }
    return arr;
}

//设置通路
function setMap(mapArr, index) {
    //边界判断
    if (index.i >= maxHCount || index.j >= maxWCount || index.i < 0 || index.j < 0) return null;
    //是否设置过
    if (mapArr[index.i][index.j]) return null;

    //设置地图
    mapArr[index.i][index.j] = 1;

    let nearArr = [];
    for (let dir of dirArr) {
        let i = dir.i + index.i;
        let j = dir.j + index.j;

        //边界判断
        if (i >= maxHCount || j >= maxWCount || i < 0 || j < 0) continue;

        if (!mapArr[i][j]) {
            nearArr.push({ i, j });
        }
    }

    return nearArr;
}

module.exports = {
    getRandNameArr
};